package com.kakarote.examine.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.google.common.collect.Lists;
import com.kakarote.common.log.annotation.OperateLog;
import com.kakarote.common.log.entity.OperationLog;
import com.kakarote.common.log.entity.OperationResult;
import com.kakarote.common.log.enums.ApplyEnum;
import com.kakarote.common.log.enums.BehaviorEnum;
import com.kakarote.common.log.enums.OperateObjectEnum;
import com.kakarote.common.log.enums.OperateTypeEnum;
import com.kakarote.core.common.ApiExplain;
import com.kakarote.core.common.Result;
import com.kakarote.core.common.enums.ExamineLabelEnum;
import com.kakarote.core.common.enums.ExamineSystemEnum;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.feign.crm.entity.ExamineField;
import com.kakarote.core.feign.examine.entity.*;
import com.kakarote.examine.constant.ExamineTypeEnum;
import com.kakarote.examine.entity.BO.ExaminePageBO;
import com.kakarote.examine.entity.BO.ExaminePreviewBO;
import com.kakarote.examine.entity.PO.Examine;
import com.kakarote.examine.entity.VO.ExamineVO;
import com.kakarote.examine.mapper.ExamineMapper;
import com.kakarote.examine.service.IExamineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 审批表 前端控制器
 * </p>
 *
 * @author zhangzhiwei
 * @since 2020-11-13
 */
@RestController
@RequestMapping("/examines")
@Api(tags = "审批中心接口")
public class ExamineController {

    @Resource  IExamineService examineService;
    @Resource ExamineMapper examineMapper;


    @PostMapping("/queryField")
    @ApiOperation("查询审批可供设置的字段")
    public Result<List<ExamineField>> queryField(@RequestParam("label") Integer label, @RequestParam(value = "categoryId", required = false) Long categoryId) {
        List<ExamineField> fieldList = examineService.queryField(label, categoryId);
        return Result.ok(fieldList);
    }

    @PostMapping("/queryExamineById")
    @ApiOperation("查询审批详情")
    public Result<Examine> queryExamineById(@RequestParam("examineId") Long examineId) {
        Examine byId = examineService.getById(examineId);
        Long examineInitId = byId.getExamineInitId();

        //获取最新审批信息
        Examine one = examineService.lambdaQuery().eq(Examine::getExamineInitId, examineInitId)
                .orderByDesc(Examine::getExamineId).last("limit 1").one();
        return Result.ok(one);
    }

    @PostMapping("/queryList/{type}")
    @ApiExplain("业务审批流列表页")
    public Result<BasePage<ExamineVO>> queryList(@PathVariable("type") Integer type, @RequestBody ExaminePageBO examinePageBo) {
        List<Integer> labelList = ExamineSystemEnum.listLabel(type);
        if(ObjectUtils.isEmpty(labelList)) {
            return Result.ok(new BasePage<>());
        }
        examinePageBo.setLabelList(labelList);
        examinePageBo.setIsPart(true);
        BasePage<ExamineVO> voBasePage = examineService.queryList(examinePageBo);
        return Result.ok(voBasePage);
    }


    @PostMapping("/queryAllList/{type}")
    @ApiOperation("查询全部审批流列表不带分页")
    public Result<List<ExamineVO>> queryAllList(@PathVariable("type") Integer type, @RequestBody ExaminePageBO examinePageBo) {
        int five = 5;
        int six = 6;
        //客户管理系统流程
        if (Objects.equals(1, type)) {
            examinePageBo.setLabelList(Arrays.asList(1, 2, 3));
        //hrm系统流程
        } else if (Objects.equals(five, type)) {
            examinePageBo.setLabelList(Lists.newArrayList(4,91));
        //进销存系统
        } else if (Objects.equals(six, type)) {
            examinePageBo.setLabelList(Arrays.asList(5, 6, 7, 8, 9, 10, 11, 12));
        } else if (Objects.equals(0, type)) {
            examinePageBo.setLabel(0);
        } else {
            return Result.ok(new ArrayList<>());
        }
        examinePageBo.setIsPart(false);
        List<ExamineVO> examineTreeVOList = examineService.queryAllList(examinePageBo);
        return Result.ok(examineTreeVOList);
    }

    @PostMapping("/queryExamineAdvanceConfig")
    @ApiOperation("查询审核信息高级配置")
    public Result<ExamineAdvancedConfigVO> queryExamineAdvanceConfig(@RequestParam("examineId") Long examineId) {
        ExamineAdvancedConfigVO examineAdvancedConfigVO = examineService.queryAdvanceConfig(examineId);
        return Result.ok(examineAdvancedConfigVO);
    }

    @PostMapping("/queryPartList")
    @ApiOperation("查询正常审批流列表")
    public Result<BasePage<ExamineVO>> queryPartList(@RequestBody ExaminePageBO examinePageBo) {
        examinePageBo.setIsPart(true);
        BasePage<ExamineVO> voBasePage = examineService.queryList(examinePageBo);
        return Result.ok(voBasePage);
    }

    @PostMapping("/queryNormalExamine")
    @ApiExplain("查询可用审批个数")
    public Result<List<Examine>> queryNormalExamine(@RequestParam("label") Integer label) {
        List<Examine> examineList = examineService.lambdaQuery().eq(Examine::getLabel, label).eq(Examine::getStatus, 1).list();
        return Result.ok(examineList);
    }

    @PostMapping("/queryAllNormalExamine")
    @ApiExplain("查询可用审批个数")
    public Result<List<Examine>> queryNormalExamine() {
        List<Examine> examineList = examineService.lambdaQuery().eq(Examine::getStatus, 1).list();
        return Result.ok(examineList);
    }

    @PostMapping("/updateStatus")
    @ApiOperation("修改审批状态")
    @OperateLog(type = OperateTypeEnum.SETTING)
    public Result updateStatus(@RequestParam("status") Integer status, @RequestParam("examineId") Long examineId) {
        Examine examine = examineService.getById(examineId);
        OperationLog operationLog = new OperationLog();
        if (examine.getLabel() == 0) {
            operationLog.setApply(ApplyEnum.OFFICE_MANAGEMENT);
            operationLog.setApplyObject(OperateObjectEnum.APPROVAL_FLOW);
        } else if (ListUtil.toList(1, 2, 3).contains(examine.getLabel())) {
            operationLog.setApply(ApplyEnum.CUSTOMER_MANAGEMENT);
            operationLog.setApplyObject(OperateObjectEnum.BUSINESS_APPROVAL_FLOW);
        } else if (examine.getLabel() == 4) {
            operationLog.setApply(ApplyEnum.HUMAN_RESOURCE_MANAGEMENT);
            operationLog.setApplyObject(OperateObjectEnum.HUMAN_APPROVAL_FLOW);
        } else if (ListUtil.toList(5, 6, 7, 8, 9, 10, 11, 12).contains(examine.getLabel())) {
            operationLog.setApply(ApplyEnum.JXC_MANAGEMENT);
            operationLog.setApplyObject(OperateObjectEnum.JXC_APPROVAL_FLOW);
        }
        operationLog.setOperationObject(examine.getExamineName());
        if (Objects.equals(1, status)) {
            operationLog.setBehavior(BehaviorEnum.START);
        } else if (Objects.equals(2, status)) {
            operationLog.setBehavior(BehaviorEnum.STOP);
        } else if (Objects.equals(3, status)) {
            operationLog.setBehavior(BehaviorEnum.DELETE);
        }
        operationLog.setOperationInfo(operationLog.getBehavior().getName() + "：" + examine.getExamineName());
        return OperationResult.ok(examineService.updateStatus(examineId, status, true), ListUtil.toList(operationLog));
    }

    @PostMapping("/addExamine")
    @ApiOperation("保存业务审批流")
    @OperateLog(type = OperateTypeEnum.SETTING)
    public Result<ExamineInfoVo> addExamine(@RequestBody ExamineSaveBO examineSaveBO) {
        Examine examine = examineService.addExamine(examineSaveBO);
        OperationLog operationLog = new OperationLog();
        operationLog.setOperationObject(examine.getExamineName());
        if (Objects.isNull(examineSaveBO.getExamineId())) {
            operationLog.setBehavior(BehaviorEnum.SAVE);
        } else {
            operationLog.setBehavior(BehaviorEnum.UPDATE);
        }
        operationLog.setOperationInfo(operationLog.getBehavior().getName() + "审批流程：" + examine.getExamineName());
        //1.办公流程
        if (examine.getLabel() == ExamineLabelEnum.OA_办公管理.getCode()) {
            operationLog.setApply(ApplyEnum.OFFICE_MANAGEMENT);
            operationLog.setApplyObject(OperateObjectEnum.APPROVAL_FLOW);
        //业务流程-CRM流程
        } else if (ExamineLabelEnum.getCrmCode().contains(examine.getLabel())) {
            operationLog.setApply(ApplyEnum.CUSTOMER_MANAGEMENT);
            operationLog.setApplyObject(OperateObjectEnum.BUSINESS_APPROVAL_FLOW);
        //业务流程-HRM流程
        } else if (ExamineLabelEnum.getHrmCode().contains(examine.getLabel())) {
            operationLog.setApply(ApplyEnum.HUMAN_RESOURCE_MANAGEMENT);
            operationLog.setApplyObject(OperateObjectEnum.HUMAN_APPROVAL_FLOW);
        //业务流程-JXC流程
        } else if (ExamineLabelEnum.getJxcCode().contains(examine.getLabel())) {
            operationLog.setApply(ApplyEnum.JXC_MANAGEMENT);
            operationLog.setApplyObject(OperateObjectEnum.JXC_APPROVAL_FLOW);
        } else {
            operationLog = null;
        }
        return OperationResult.ok(BeanUtil.copyProperties(examine, ExamineInfoVo.class), ListUtil.toList(operationLog));
    }

    @PostMapping("/queryExamineFlow")
    @ApiOperation("获取审批详情")
    public Result<List<ExamineFlowVO>> queryExamineFlow(@RequestParam("examineId") Long examineId) {
        List<ExamineFlowVO> examineFlowVOList = examineService.queryExamineFlow(examineId);
        return Result.ok(examineFlowVOList);
    }


    @PostMapping("/previewFiledName")
    @ApiOperation("获取审批流程条件")
    public Result<List<ExamineFlowConditionDataVO>> previewFiledName(@RequestBody ExaminePreviewBO examinePreviewBO) {
        List<ExamineFlowConditionDataVO> filedNameList = examineService.previewFiledName(examinePreviewBO.getLabel(), examinePreviewBO.getRecordId(), examinePreviewBO.getExamineId());
        return Result.ok(filedNameList);
    }

    @ApiOperation("查询审批人员控件字段")
    @PostMapping("/queryExamineUserField")
    public Result<List<Map<String, Object>>> queryExamineUserField(@RequestBody ExaminePreviewBO examinePreviewBO) {
        List<Map<String, Object>> examineUserFieldVOList = examineService.queryExamineUserField(examinePreviewBO.getLabel(), examinePreviewBO.getExamineId());
        return Result.ok(examineUserFieldVOList);
    }

    @PostMapping("/queryExamineAdvancedConfig")
    @ApiOperation("查询审批高级配置")
    public Result<ExamineAdvancedConfigVO> queryExamineAdvancedConfig(@RequestBody ExaminePreviewBO examinePreviewBO) {
        ExamineAdvancedConfigVO examineAdvancedConfigVO = examineService.queryExamineAdvancedConfig(examinePreviewBO);
        return Result.ok(examineAdvancedConfigVO);
    }


    @PostMapping("/previewExamineFlow")
    @ApiOperation("预览审批流程")
    public Result<ExaminePreviewVO> previewExamineFlow(@RequestBody ExaminePreviewBO examinePreviewBO) {
        ExaminePreviewVO examineFlowVO = examineService.previewExamineFlow(examinePreviewBO);
        return Result.ok(examineFlowVO);
    }

    @PostMapping("/queryExamineFlowInfo")
    @ApiExplain("获取审批详情")
    public Result<ExaminePreviewVO> queryExamineFlowInfo(@RequestParam("examineId") Long examineId) {
        ExaminePreviewVO examinePreviewVO = examineService.queryExamineFlowInfo(examineId);
        return Result.ok(examinePreviewVO);
    }

    @ApiOperation("查询所有字段语言包key信息")
    @PostMapping(value = "/getAllFieldLanguageRel")
    public Result<List<Map<String, Object>>> getAllFieldLanguageRel() {
        return Result.ok(examineService.getAllFieldLanguageRel());
    }

    @ApiOperation("查询历史审批ID")
    @PostMapping(value = "/queryHistoryExamineId")
    public Result<List<Long>> queryHistoryExamineId(@RequestParam("examineId") Long examineId) {
        Examine examine = examineService.getById(examineId);
        if (examine == null) {
            return Result.ok(new ArrayList<>());
        }
        List<Examine> list = examineService.lambdaQuery().in(Examine::getExamineInitId, examine.getExamineInitId()).list();
        List<Long> collect = list.stream().map(Examine::getExamineId).collect(Collectors.toList());
        return Result.ok(collect);
    }

    @ApiOperation("查询某个业务流程")
    @PostMapping(value = "/getCompanyBizExamine")
    public Result<ExamineInfoVo> getCompanyBizExamine(@RequestParam("label") Integer label,@RequestParam("deptId") Long deptId) {
        ExamineInfoVo examine = examineService.getBizExamine(label,deptId );
        return Result.ok(examine);
    }

}

